﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkbookDesigner.Addons.SpreadsheetEditor.Model;

namespace WorkbookDesigner.Addons.SpreadsheetEditor.UI
{
    public partial class PromptSetForm : Form
    {
        public PromptSetForm(PromptModel pm)
        {
            InitializeComponent();
            Pm = pm;
            Init();
        }

        public bool VisiblePrompt
        {
            get { return checkBoxVisible.Checked; }
            private set { checkBoxVisible.Checked = value; }
        }

        private string PromptName
        {
            get
            {
                return GetPValue(ucName, true);
            }
            set
            {
                ucName.PValue = value;
            }
        }

        private string Value
        {
            get
            {
                return GetPValue(ucValue, true);
            }
            set
            {
                ucValue.PValue = value;
            }
        }

        private string ControlType
        {
            get
            {
                return GetPValue(uctControlType, true);
            }
            set
            {
                uctControlType.PValue = value;
            }
        }

        private string HelpMessage
        {
            get
            {
                return GetPValue(ucHelpMessage, true);
            }
            set
            {
                ucHelpMessage.PValue = value;
            }
        }

        private string VerifyCode
        {
            get
            {
                return GetPValue(ucVerityCode, true);
            }
            set
            {

                ucVerityCode.PValue = value;
            }
        }

        private string ComboString
        {
            get
            {
                return GetPValue(ucComboString, true);
            }
            set
            {
                ucComboString.PValue = value;
            }
        }

        private string Color
        {
            get
            {
                return GetPValue(ucColor, true);
            }
            set
            {
                ucColor.PValue = value;
            }
        }

        private string Picture
        {
            get
            {
                return GetPValue(ucPicture, true);
            }
            set
            {
                ucPicture.PValue = value;
            }
        }

        private string VisibleRange
        {
            get
            {
                return GetPValue(ucVisibleRange, true);
            }
            set
            {
                ucVisibleRange.PValue = value;
            }
        }

        private string HideInReport
        {
            get
            {
                return GetPValue(ucHideInReport, true);
            }
            set
            {
                ucHideInReport.PValue = value;
            }
        }

        private string PromptTabIndex
        {
            get
            {
                return GetPValue(ucTabIndex, true);
            }
            set
            {
                ucTabIndex.PValue = value;
            }
        }

        private string CalculatorIndex
        {
            get
            {
                return GetPValue(ucCalIndex, true);
            }
            set
            {
                ucCalIndex.PValue = value;
            }
        }

        private PromptModel Pm { get; }


        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 获取控件文本框的值,可选是否验证勾选框状态
        /// 如验证则根据控件勾选框结果返回值:True返回Value,False返回Null.
        /// 不验证则直接返回value
        /// </summary>
        /// <param name="bp">要取值的控件</param>
        /// <param name="validate">是否执行验证</param>
        /// <returns></returns>
        private static string GetPValue(UcBasePrompt bp, bool validate)
        {
            //if ((validate && bp.Adaptable) || !validate)
            if (validate)
                return bp.Adaptable ? bp.PValue : null;
            return bp.PValue;
        }

        /// <summary>
        /// 获取包含所有文本框变量值的新实例(不包含勾选框校验,返回所有文本框中的值)
        /// </summary>
        /// <returns></returns>
        public PromptModel GetPrompt()
        {
            PromptModel pm = new PromptModel()
            {
                Name = GetPValue(ucName, false),
                Value = GetPValue(ucValue, false),
                ControlType = GetPValue(uctControlType, false),
                HelpMessage = GetPValue(ucHelpMessage, false),
                VerifyCode = GetPValue(ucVerityCode, false),
                ComboString = GetPValue(ucComboString, false),
                Color = GetPValue(ucColor, false),
                Picture = GetPValue(ucPicture, false),
                VisibleRange = GetPValue(ucVisibleRange, false),
                HideInReport = GetPValue(ucHideInReport, false),
                TabIndex = GetPValue(ucTabIndex, false),
                CalculatorIndex = GetPValue(ucCalIndex, false),
                VisiblePrompt = VisiblePrompt,
            };
            return pm;
        }

        private void Init()
        {
            PromptName = Pm.Name;
            Value = Pm.Value;
            ControlType = Pm.ControlType;
            HelpMessage = Pm.HelpMessage;
            VerifyCode = Pm.VerifyCode;
            ComboString = Pm.ComboString;
            Color = Pm.Color;
            Picture = Pm.Picture;
            VisibleRange = Pm.VisibleRange;
            HideInReport = Pm.HideInReport;
            PromptTabIndex = Pm.TabIndex;
            CalculatorIndex = Pm.CalculatorIndex;
            VisiblePrompt = Pm.VisiblePrompt;
        }

        /// <summary>
        /// 同步当前窗口中的各项变量值至指定的变量实例中
        /// 包含了勾选框校验,未勾选的变量值将不做改动
        /// </summary>
        /// <param name="pm">需要同步的变量实例</param>
        public void SyncPrompt(PromptModel pm)
        {
            pm.Name = string.IsNullOrEmpty(PromptName) ? pm.Name : PromptName;
            pm.Value = Value ?? pm.Value;
            pm.ControlType = ControlType ?? pm.ControlType;
            pm.HelpMessage = HelpMessage ?? pm.HelpMessage;
            pm.VerifyCode = VerifyCode ?? pm.VerifyCode;
            pm.ComboString = ComboString ?? pm.ComboString;
            pm.Color = Color ?? pm.Color;
            pm.Picture = Picture ?? pm.Picture;
            pm.VisibleRange = VisibleRange ?? pm.VisibleRange;
            pm.HideInReport = HideInReport ?? pm.HideInReport;
            pm.TabIndex = PromptTabIndex ?? pm.TabIndex;
            pm.CalculatorIndex = CalculatorIndex ?? pm.CalculatorIndex;
            pm.VisiblePrompt = pm.VisiblePrompt;
        }

    }
}
